﻿using EasyNet.Componets.Core.Zips;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Zips
{
    public class MicrosoftCompressZip : IZip
    {
        #region 压缩
        public MemoryStream Compression(List<string> files)
        {
            MemoryStream ms = new MemoryStream();
            Compression(ms, files, ZipArchiveMode.Create);
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }

        public void Compression(List<string> files, string zipPath)
        {
            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }
            using (var fs = new FileStream(zipPath, FileMode.Create))
            {
                Compression(fs, files, ZipArchiveMode.Update);
            }
        }

        private void Compression(Stream stream, List<string> files, ZipArchiveMode model)
        {
            using (var archive = new ZipArchive(stream, model, true))
            {
                foreach (var file in files)
                {
                    if (!string.IsNullOrEmpty(file) && File.Exists(file))
                    {
                        var entryName = Path.GetFileName(file);
                        var zipEntry = archive.CreateEntryFromFile(file, entryName);
                    }
                }
            }
        }

        #endregion

        #region 提取
        public void Extract(string zipPath, string extractPath)
        {
            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Read))
            {
                archive.ExtractToDirectory(extractPath);
            }
        }
        #endregion

    }
   
}
